package bookworm;

/**
 * this class is responsible for counting how many parallelThreads are active in our 
 * system. Sometimes, we want to distribute work through threads in our system and we also
 * wish the system stops and waits until they're finished. This counter will help us with this.
 * This class is a Singleton because we don't want more than one instance of this counter
 * (it could lead the system to work with false information about working threads).
 * 
 *
 */
public class ParallelThreadsCounter 
{
	private static ParallelThreadsCounter singletonParallelThreadsCounter;
	private static int howManyThreadsAreActive;
	
	private ParallelThreadsCounter()
	{
		howManyThreadsAreActive = 0;
	}
	
	
	public synchronized static ParallelThreadsCounter getParallelThreadsCounter()
	{
		if(singletonParallelThreadsCounter == null)
		{
			singletonParallelThreadsCounter = new ParallelThreadsCounter();
		}
		
		return singletonParallelThreadsCounter;
	}
	
	public synchronized int getHowManyThreadsAreActive()
	{
		return howManyThreadsAreActive;
	}
	
	public synchronized void decreaseCounterThreadsActive()
	{
		howManyThreadsAreActive = howManyThreadsAreActive - 1;
	}
	
	public synchronized void increaseCounterThreadsActive()
	{
		howManyThreadsAreActive = howManyThreadsAreActive + 1;
	}

}
